﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using ZilLion.Service.Core.Domain.EntityBase;
using ZilLion.Service.WeChatMall.Domain.Entitys.ShopInfo;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface;
using ZilLion.Service.WeChatMall.Domain.Requests;
using ZilLion.Service.WeChatMall.Domain.Responses;
using ZilLion.Service.WeChatMall.Domain.Responses.Shopinfo;
using ZilLion.Service.WeChatMall.Domain.Services.Interface;

namespace ZilLion.Service.WeChatMallOss.Controllers
{
    /// <summary>
    ///     微商城后台店铺设置
    /// </summary>
    [Route("Api/WechatMallOss/Shop")]
    public class WeChatMallShopSetController : WechatMallOssControllerBase
    {
        private readonly IGdMapRepository _gdMapRepository;
        private readonly IShopInfoService _shopInfoService;

        public WeChatMallShopSetController(IShopInfoService shopInfoService, IGdMapRepository gdMapRepository)
        {
            _shopInfoService = shopInfoService;
            _gdMapRepository = gdMapRepository;
        }

        /// <summary>
        ///     批量保存门店下属货品关系
        /// </summary>
        /// <returns></returns>
        [Route("SaveShopGsRelationShip")]
        [HttpPost]
        public ResponseBase SaveShopGsRelationShip([FromBody]ShopGsRelationShipRequest request, string token = null)
        {
            var relationList = new List<ShopGsRelationShipEntity>();
            foreach (var gsid in request.Gsids)
            {
                var entity = new ShopGsRelationShipEntity
                {
                    ShopId = request.ShopId,
                    GsId = gsid,
                    Updater = GetUserId(),
                    UpdatedTime = DateTime.Now.GetNowChinaTime()
                };
                relationList.Add(entity);
            }
            _shopInfoService.SaveShopGsRelationShip(relationList);
            return new ResponseBase();
        }

        /// <summary>
        ///     根据shopid 获取shop信息
        /// </summary>
        /// <param name="shopid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetShop")]
        [HttpGet]
        public ShopInfoResponse GetShop(long shopid, string token = null)
        {
            var dtos = _shopInfoService.GetShopById(shopid);
            return new ShopInfoResponse {ShopInfo = dtos};
        }

        /// <summary>
        ///     根据appid 获取Shop信息
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetShopsByAppId")]
        [HttpGet]
        public ShopInfoListResponse GetShopsByAppId(string appid, string token = null)
        {
            if (!appid.IsNotNullOrEmpty()) return new ShopInfoListResponse {ShopInfos = new List<ShopInfoDto>()};
            var dtos = _shopInfoService.GetShopsByAppid(appid);
            return new ShopInfoListResponse {ShopInfos = dtos};
        }

        /// <summary>
        ///     根据关键字和城市编码获取模糊匹配的地址
        /// </summary>
        /// <param name="citycode"></param>
        /// <param name="token"></param>
        /// <param name="keywords"></param>
        /// <returns></returns>
        [Route("SerchByKeywords")]
        [HttpGet]
        public MapSerchResponse SerchByKeywords(string keywords, string citycode = null, string token = null)
        {
            var tmp = _gdMapRepository.SerchMap(citycode, keywords);
            foreach (var tip in tmp.Tips)
            {
                if (tip.Address == null || tip.Address.ToString() == "[]")
                    tip.Address = string.Empty;
                if (tip.Id == null || tip.Id.ToString() == "[]")
                    tip.Id = string.Empty;

                if (tip.Location == null || tip.Location.ToString() == "[]")
                    tip.Location = string.Empty;
            }
            return tmp;
        }

        /// <summary>
        ///     保存店铺信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("SaveShopInfo")]
        [HttpPost]
        public ShopInfoResponse SaveShopInfo([FromBody] ShopInfoRequest request, string token = null)
        {
            var entitys = request.SplitToEntity();
            var basic = entitys.Item1;
            var relation = entitys.Item2;
            var supports = entitys.Item3;
            if (basic != null)
            {
                basic.ZlCompanyKey = GetZlCompanyKey();
                basic.Updater = GetUserId();
                basic.UpdatedTime = DateTime.Now.GetNowChinaTime();
            }
            if (relation != null)
            {
                relation.Updater = GetUserId();
                relation.UpdatedTime = DateTime.Now.GetNowChinaTime();
            }
            if (supports != null)
                foreach (var support in supports)
                {
                    support.Updater = GetUserId();
                    support.UpdatedTime = DateTime.Now.GetNowChinaTime();
                }
            var resultdto = _shopInfoService.SaveShopInfo(basic, relation, supports);
            return new ShopInfoResponse {ShopInfo = resultdto};
        }


        /// <summary>
        ///     删除店铺信息
        /// </summary>
        /// <param name="shopid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("deleteShop")]
        [HttpDelete]
        public ResponseBase DeleteShop(long shopid, string token = null)
        {
            _shopInfoService.DeleteShop(shopid);
            return new ResponseBase();
        }


        /// <summary>
        ///     获取可以导入的线下货品
        /// </summary>
        /// <returns></returns>
        [Route("GetImportableShops")]
        [HttpGet]
        public OfflineShopResponse GetImportableShops()
        {
            var zlcompnatkey = GetZlCompanyKey();
            var list = _shopInfoService.GetImportableShops(zlcompnatkey);
            return new OfflineShopResponse {List = list};
        }

        /// <summary>
        ///     批量导入线下货品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("ImportOfflineShops")]
        [HttpPost]
        public ResponseBase ImportOfflineShops([FromBody]ImportOfflineShopRequest request)
        {
            var list = request.ShopList;
            var appId = request.Appid;
            var zlCompanyKey = GetZlCompanyKey();
            var updater = GetUserId();
            _shopInfoService.ImportOfflineShops(list, zlCompanyKey, updater, appId);
            return new ResponseBase();
        }
    }
}